﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseData;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class SellerEmployeeRoleController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        #region 销售人员角色列表 JsonResult Index(BaseQueryParams model)
        [HttpPost]
        public JsonResult Index(BaseQueryParams model)
        {
            var total = 0;
            var list = ServiceFactory.SellerEmployeeRoleServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.RoleName,
                o.RoleCode,
                o.Intro,
                o.IsAvailable
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region 根据id判断 是添加还是修改  获取销售人员详情信息 ActionResult Edit(int id)
        public ActionResult Edit(int id)
        {
            ViewBag.ShowTitle = id > 0 ? "编辑角色" : "添加角色";
            var productBrand = ServiceFactory.SellerEmployeeRoleServiceInstance.LoadByID(id);
            return View(productBrand);
        }
        #endregion
        #region 保存销售人员信息  ActionResult Save(SellerEmployeeRole model)
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Save(SellerEmployeeRole model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            var isUpdate = model.ID > 0;
            if (model.ID > 0)
            {
                var entity = ServiceFactory.SellerEmployeeRoleServiceInstance.LoadByID(model.ID);
                if (entity != null)
                {
                    if (ServiceFactory.SellerEmployeeRoleServiceInstance.IsExistRoleName(model.RoleName, model.ID))
                    {
                        responseResult.Message = "已存在同名的角色，请修改角色名";
                    }
                    else if (ServiceFactory.SellerEmployeeRoleServiceInstance.IsExistRoleCode(model.RoleCode, model.ID))
                    {
                        responseResult.Message = "已存在同名的角色代码，请修改角色名代码";
                    }
                    else
                    {
                        entity = ReflectHelper<SellerEmployeeRole, SellerEmployeeRole>.CloneT1ToT2(model, entity);
                        responseResult.Success = ServiceFactory.SellerEmployeeRoleServiceInstance.UpdateEntities(entity);
                    }
                }
                else
                {
                    responseResult.Message = "该角色不存在或已被删除";
                }
            }
            else
            {
                if (ServiceFactory.SellerEmployeeRoleServiceInstance.IsExistRoleName(model.RoleName))
                {
                    responseResult.Message = "已存在同名的角色，请修改角色名";
                }
                else if (ServiceFactory.SellerEmployeeRoleServiceInstance.IsExistRoleCode(model.RoleCode))
                {
                    responseResult.Message = "已存在同名的角色代码，请修改角色名代码";
                }
                else
                {
                    model.CreateBy = CurrentUser.UserAccount;
                    model.CreateTime = DateTime.Now;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var id = ServiceFactory.SellerEmployeeRoleServiceInstance.AddEntities(model).ID;
                    responseResult.Success = id > 0;
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add && responseResult.Success ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ID });
        }
#endregion
        #region 禁用或启用 销售人员  JsonResult UpdateStatus(int id, bool isAvailable)
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.SellerEmployeeRoleServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                success = ServiceFactory.SellerEmployeeRoleServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
       #endregion
    }
}